﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Formality
{
    public sealed class QueryCache
    {
        public static QueryCache<T> New<T>(IEnumerable<T> Seq)
        {
            return new QueryCache<T>(Seq);
        }
    }

    public class QueryCache<T> : IEnumerable<T>
    {
        public QueryCache(IEnumerable<T> Seq) { this.Seq = Seq; }

        public IEnumerator<T> GetEnumerator()
        {
            if (Cached == null)
                Cached = Seq.ToList();

            return Cached.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); }

        private IEnumerable<T> Seq;
        private IEnumerable<T> Cached;
    }
}
